package com.example.administrator.shzs.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.shzs.R;
import com.example.administrator.shzs.db.GoodsBase;
import com.example.administrator.shzs.db.GoodsBean;
import com.example.administrator.shzs.db.GoodsDataBaseInterface;
import com.example.administrator.shzs.db.OperateGoodsDataBase;

import java.util.List;
/**
 * 购物车Adapter
 */
public class GoodsCarAdapter extends RecyclerView.Adapter<GoodsCarAdapter.MyViewHolder> {

    private GoodsDataBaseInterface mGoodsDataBaseInterface;
    private List<GoodsBean> goodsBeens;
    private Context context;
    public GoodsCarAdapter(Context context,List<GoodsBean> goodsBeens) {
        this.context = context;
        this.goodsBeens=goodsBeens;
        mGoodsDataBaseInterface= OperateGoodsDataBase.getInstance();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view=View.inflate(context,R.layout.goods_car_item,null);

        MyViewHolder holder=new MyViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        String goodsName=goodsBeens.get(position).getGoodsName();
        String goodsPrice=goodsBeens.get(position).getGoodsPrice();
        String goodsNum=goodsBeens.get(position).getGoodsNum();
        int goodsid=goodsBeens.get(position).getGoodsId();
        int shopid=goodsBeens.get(position).getShopid();

        holder.tvGoodsName.setText(goodsName);
        holder.tvGoodsPrice.setText("单价￥: "+goodsPrice);
        holder.tvGoodsNum.setText(goodsNum);



        //先获取单个存储的商品数量
        if(mGoodsDataBaseInterface.getGoodsNumber(context,shopid,goodsid) ==0 ){

            holder.tvGoodsNum.setVisibility(View.GONE);
            holder.ivJian.setVisibility(View.GONE);
            //holder.rlClear.setVisibility(View.GONE);
        }else {
            //先获取单个商品数量
            holder.tvGoodsNum.setText(""+mGoodsDataBaseInterface.getGoodsNumber(context,shopid,goodsid));
            holder.tvGoodsNum.setVisibility(View.VISIBLE);
            holder.ivJian.setVisibility(View.VISIBLE);
        }

        //加号、减号按钮监听
        setOnListtener(holder,goodsid,goodsName,goodsPrice);
    }

    /**
     * 删除单条Item数据
     * @param position
     */
    public void removeData(int position) {
        goodsBeens.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public int getItemCount() {
        return goodsBeens == null ? 0 :goodsBeens.size();
    }

    private void setOnListtener(final MyViewHolder holder, final int goodsid, final String goodsName, final String goodsPrice) {

        if (mOnItemClickListener != null) {

            //给加号按钮注册点击事件
            holder.ivjia.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mOnItemClickListener.OnjiaClick(holder,goodsid,goodsName,goodsPrice);
                }
            });

        }


        if(mOnItemClickListener != null) {
            holder.ivJian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mOnItemClickListener.OnJianClick(holder,goodsid,goodsName,goodsPrice);

                }
            });
        }
    }


    public class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView tvGoodsName;
        public TextView tvGoodsPrice;
        public TextView tvGoodsNum;
        public ImageView ivjia;
        public ImageView ivJian;
        private RelativeLayout rlClear;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvGoodsName= (TextView) itemView.findViewById(R.id.tv_goods_name);
            tvGoodsPrice= (TextView) itemView.findViewById(R.id.tv_goods_price);
            tvGoodsNum= (TextView) itemView.findViewById(R.id.tv_goods_num);
            ivjia= (ImageView) itemView.findViewById(R.id.iv_jia);
            ivJian= (ImageView) itemView.findViewById(R.id.iv_jian);
            rlClear= (RelativeLayout) itemView.findViewById(R.id.rl_clear_goods_car);
        }

    }


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }



    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    /**
     * item监听器
     */
    public static interface OnRecyclerViewItemClickListener {
        void OnjiaClick(MyViewHolder holder,int goodsid,String goodsName,String goodsPrice);
        void OnJianClick(MyViewHolder holder,int goodsid,String goodsName,String goodsPrice);

    }


}
